<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 11.05.12
  Time: 18:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "../index.jsp";
        response.sendRedirect(redirectURL);
    }

    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>See accounts</title>
    <link rel="stylesheet" type="text/css" href="style"/>
    <%
        String owner_id = request.getParameter("ID");
        String type = request.getParameter("type");
        boolean isOk = false;
        if (owner_id != null && type!=null) {
            ArrayList<Account> accounts = dbConnection.getAccounts(user_id,Integer.parseInt(owner_id), DBConnection.OwnerType.valueOf(type));
            isOk = true;
        }
    %>
</head>
<body>
<div class="logout" style="text-align:right;"><a href="../logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><b><a href="operations.jsp">Operations</a></b></li>
            <li><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></li>
        </ul>
    </div>
    <div class="content">
        <form method="post" action="see_accounts.jsp">
            <table class="table">
                <tr>
                    <td width="150">Owner ID: <font color="red">*</font></td>
                    <td><input type="text" name="ID"><td>
                    <td></td>
                </tr>
                <td>Type: <font color="red">*</font></td>
                <td>
                    <select name="type">
                        <option> CLIENT </option>
                        <option> ORGANIZATION </option>
                    </select>
                </td>
            <td><input type="submit" value="Submit"></td>
            <%  if (isOk) {
                    ArrayList<Account> accounts = dbConnection.getAccounts(user_id,Integer.parseInt(owner_id), DBConnection.OwnerType.CLIENT);
                    if (accounts.size() == 0) {
                    %> No accounts found!!! <%
                    } else for (Account account : accounts) {  %>
                    <table class="table" >
                    <tr>
                        <td>Number:</td>
                        <td><%= account.number%></td>
                    </tr>
                    <tr>
                        <td>Amount:</td>
                        <td><%= account.money%></td>
                    </tr>
                    <tr>
                        <td>Date opened:</td>
                        <td><%= account.dateOpened%></td>
                    </tr>
                    <% if (account.dateClosed != null) %>
                    <tr>
                        <td>Date closed:</td>
                        <td><%= account.dateClosed%></td>
                    </tr>
                    <%%>
                    <hr width="400" align="left"/>
                    </table>
                    <a href="see_account_history.jsp?account_id=<%=account.number%>">Account history</a>
            <%      }
                }
            %>
        </form>
    </div>
</div>
</body>
</html>